<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thanhnv
 * Date: 11/25/13
 * Time: 3:28 PM
 * To change this template use File | Settings | File Templates.
 */

namespace Core\Helper;


class MenuHelper {

    //exclude root node.
    public static function getHtmlTree($rows, $ulId,$refMenuArr=array(), $selectedCssClass=null){
        $right = array();
        $result = "<ul id='".$ulId."'>";

        foreach ($rows as $item) {
            if(count($right)>0){
                while (count($right)>0 && $right[count($right)-1]['rgt']<$item['rgt']) {
                    if($right[count($right)-1]['lft'] < $right[count($right)-1]['rgt'] - 1){
                        $result = $result.'</ul></li>';
                    }
                    array_pop($right);
                }
            }
            $liClass='';
            foreach ($refMenuArr as $refMenuItem) {
                if($item['id'] == $refMenuItem->id)
                    $liClass = $selectedCssClass;
            }

            $result = $result.'<li class ="'.$liClass.'" ><a href="'/*.Core_Tool::getBaseUrl($item["route"]).$item["params"]*/.'">'.$item["label"].'</a>';
            if($item['lft'] < $item['rgt'] -1){
                $result= $result.'<ul>';
            }else{
                $result= $result.'</li>';
            }
            array_push($right,$item);
        }
        return $result.'</ul>';
    }

    public static  function getCategoryForCombo($rows){

        $right = array();
        $result = array();

        foreach ($rows as $item) {

            if(count($right)>0){
                while ($right[count($right)-1]->rgt<$item->rgt) {
                    array_pop($right);
                }
            }
            foreach ($right as $rightItem) {
                $item->name = "-".$item->name;
            }
            array_push($result, $item);

            array_push($right,$item);
        }
        return $result;
    }

}